package com.fgsort;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.swing.JOptionPane;
 




import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;

public class ExcelWriteSymbolFile {

	public ExcelWriteSymbolFile(List<String> lstSymbols, String sExcelFile, boolean bOpenFile) {

		try {
			int nFieldCnt1 = 0;
			int nFieldCnt2 = 0;
			File exlFile = new File(sExcelFile);
			WritableWorkbook writableWorkbook = Workbook.createWorkbook(exlFile);
				
			WritableSheet ws = writableWorkbook.createSheet("PLCvsRUPLAN", 0);
			
			///header
			Label labelVer = new Label(0, 0, "iTool: v0.4 -> symbols check");
			ws.addCell(labelVer);
			Label labelBlank = new Label(0, 1, "");
			ws.addCell(labelBlank);
			Label labelPLC = new Label(0, 2, "PLC Symbols", createFormatCellStatus(jxl.format.Colour.LIGHT_GREEN));
			ws.addCell(labelPLC);
			Label labelRuPlan = new Label(5, 2, "RuPlan Symbols", createFormatCellStatus(jxl.format.Colour.LIGHT_GREEN));
			ws.addCell(labelRuPlan);
			Label labelErrors = new Label(9, 2, "Errors", createFormatCellStatus(jxl.format.Colour.LIGHT_ORANGE));
			ws.addCell(labelErrors);

			
			for (int row = 3; row < lstSymbols.size()/8+3; row++) {
				Label labelFound;
				Label labelFoundProb = null;
				
				if (!lstSymbols.get(nFieldCnt1+0).equals(lstSymbols.get(nFieldCnt1+4)) && !lstSymbols.get(nFieldCnt1+3).equalsIgnoreCase(lstSymbols.get(nFieldCnt1+7)))	{
					labelFoundProb = new Label(9, row, "Symbol+Comment", createFormatCellStatus(jxl.format.Colour.YELLOW));
					ws.addCell(labelFoundProb);
				}
				else  
					if (!lstSymbols.get(nFieldCnt1+0).equals(lstSymbols.get(nFieldCnt1+4))) {
						labelFoundProb = new Label(9, row, "Symbol", createFormatCellStatus(jxl.format.Colour.YELLOW));
						ws.addCell(labelFoundProb);
					}
				else 
					if (!lstSymbols.get(nFieldCnt1+3).equalsIgnoreCase(lstSymbols.get(nFieldCnt1+7)) && !lstSymbols.get(nFieldCnt1+3).equalsIgnoreCase("Reserve") && !lstSymbols.get(nFieldCnt1+7).equals(""))	{
						
						labelFoundProb = new Label(9, row, "Comment", createFormatCellStatus(jxl.format.Colour.YELLOW));
						ws.addCell(labelFoundProb);
					}
				else {
					
				}
						
				for (int column = 0; column < 8; column++) {
					if (column < 4)
						labelFound = new Label(column, row, lstSymbols.get(nFieldCnt1));
					else
						labelFound = new Label(column+1, row, lstSymbols.get(nFieldCnt1));

					///add the created cells to the sheet
					ws.addCell(labelFound);
					
					nFieldCnt1++;
				}
				
			}

			///Write and close the workbook
			writableWorkbook.write();
			writableWorkbook.close();
			
			///open the file after creation
			if (bOpenFile)	{
				Desktop.getDesktop().open(new File(sExcelFile));
			}
			
		} catch (IOException e) {
			//e.printStackTrace();
			JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"The excel file: " + sExcelFile + " cannot be written to.\nEither it " +
					"is already open or you must be administrator to write to this location.\nTry running iTool as Admin","Information",JOptionPane.ERROR_MESSAGE);
			
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
      	}

	}

	public WritableCellFormat createFormatCellStatus(jxl.format.Colour colour) throws WriteException{
		 WritableFont wfontStatus;
		 WritableCellFormat fCellstatus;
	    
	     wfontStatus = new WritableFont(WritableFont.createFont("Arial"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
		 fCellstatus = new WritableCellFormat(wfontStatus);
		 fCellstatus.setBackground(colour);	    

		 return fCellstatus;
	}
	
}

